package com.ken.forum.controller;

import com.ken.forum.common.AppResult;
import com.ken.forum.common.ResultCode;
import com.ken.forum.config.AppConfig;
import com.ken.forum.model.Article;
import com.ken.forum.model.ArticleReply;
import com.ken.forum.model.User;
import com.ken.forum.services.IArticleReplyService;
import com.ken.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Chooker
 * @create 2023-08-17 23:06
 */
@RestController
@Slf4j
@RequestMapping("/reply")
@Api(tags = "文章回复接口")
public class ArticleReplyController {
    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;

    @GetMapping("/getReplies")
    @ApiOperation("获取回复列表")
    public AppResult<List<ArticleReply>> getAllByBoardId(@ApiParam("文章id") @RequestParam("articleId") @NonNull Long articleId) {
        Article article = articleService.selectById(articleId);
        //判断文章是否删除
        if (article == null || article.getDeleteState() == 1) {
            // 返回错误提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断文章状态
        if (article.getState() == 1) {
            // 返回错误提示
            return AppResult.failed("文章已封帖");
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }

        return AppResult.success(articleReplies);

    }

    @PostMapping("/create")
    @ApiOperation("发布回复")
    public AppResult<List<ArticleReply>> create(@ApiParam("文章id") @RequestParam("articleId") @NonNull Long articleId,
                                                @ApiParam("回复内容") @RequestParam("content") @NonNull String content,
                                                HttpServletRequest httpServletRequest) {
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            // 返回提示
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Article article = articleService.selectById(articleId);
        //判断文章是否删除
        if (article == null || article.getDeleteState() == 1) {
            // 返回错误提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断文章状态
        if (article.getState() == 1) {
            // 返回错误提示
            return AppResult.failed("文章已封帖");
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        articleReply.setArticleId(articleId);

        articleReplyService.create(articleReply);

        return AppResult.success("发布回复成功");
    }


}
